    package com.example.shoppingcenter.Ai.XF;

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    import com.example.shoppingcenter.Config.XFConfig;
    import com.example.shoppingcenter.Service.xfService;
    import com.google.gson.Gson;
    import jakarta.annotation.Resource;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import okhttp3.Response;
    import okhttp3.WebSocket;
    import okhttp3.WebSocketListener;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    import java.util.UUID;

    @Data
    @Component
    @AllArgsConstructor
    @NoArgsConstructor
    public class BigModel extends WebSocketListener {

        private final Gson gson = new Gson();
        @Resource
        private XFConfig xf;
        // 个性化参数
        private String userId;
        private Boolean wsCloseFlag;
        @Resource
        public List<RoleContent> historyList=new ArrayList<>(); // 对话历史存储集合
        public String totalAnswer=""; // 大模型的答案汇总
        // 环境治理的重要性  环保  人口老龄化  我爱我的祖国
        public String NewQuestion = "";

        public BigModel(String userId, Boolean wsCloseFlag) {
            this.userId = userId;
            this.wsCloseFlag = wsCloseFlag;
        }

        public BigModel(String userId,Boolean wsCloseFlag,XFConfig xf) {
            this.userId = userId;
            this.wsCloseFlag = wsCloseFlag;
            this.xf = xf;
        }

        public boolean canAddHistory(){  // 由于历史记录最大上线1.2W左右，需要判断是能能加入历史
            int history_length=0;
            for(RoleContent temp:historyList){
                history_length=history_length+temp.content.length();
            }
            if(history_length>4000){
                historyList.remove(0);
                historyList.remove(1);
                historyList.remove(2);
                historyList.remove(3);
                historyList.remove(4);
                return false;
            }else{
                return true;
            }
        }
        @Override
        public void onOpen(WebSocket webSocket, Response response) {
            super.onOpen(webSocket, response);
            wsCloseFlag = false;
            MyThread myThread = new MyThread(this,webSocket);
            myThread.start();
        }

        @Override
        public void onMessage(WebSocket webSocket, String text) {
            // System.out.println(userId + "用来区分那个用户的结果" + text);
            JsonParse myJsonParse = gson.fromJson(text, JsonParse.class);
            if (myJsonParse.getHeader().getCode() != 0) {
                System.out.println("发生错误，错误码为：" + myJsonParse.getHeader().getCode());
                System.out.println("本次请求的sid为：" + myJsonParse.getHeader().getSid());
                totalAnswer = "error10000";
                wsCloseFlag = true;
//                webSocket.close(1000, "");
            }
//            System.out.println("正常地继续下一步");
            List<Text> textList = myJsonParse.getPayload().getChoices().getText();
            for (Text temp : textList) {
//                System.out.print(temp.getContent());
                totalAnswer=totalAnswer+ temp.getContent();
            }
            if (myJsonParse.getHeader().getStatus() == 2) {
                // 可以关闭连接，释放资源
//                System.out.println();
//                System.out.println("*************************************************************************************");
                if(canAddHistory()) {
                    historyList.remove(0);
                }
                RoleContent roleContent=new RoleContent();
                roleContent.setRole("assistant");
                roleContent.setContent(totalAnswer);
                historyList.add(roleContent);
                wsCloseFlag = true;
            }
        }
        @Override
        public void onFailure(WebSocket webSocket, Throwable t, Response response) {
            super.onFailure(webSocket, t, response);
            try {
                if (null != response) {
                    int code = response.code();
                    System.out.println("onFailure code:" + code);
                    System.out.println("onFailure body:" + response.body().string());
                    if (101 != code) {
                        System.out.println("connection failed");
                        System.exit(0);
                    }
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
